package com.lft.freshordering.service.impl;

import com.lft.freshordering.entity.*;
import com.lft.freshordering.entity.VO.*;
import com.lft.freshordering.mapper.OrdersMapper;
import com.lft.freshordering.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.Data;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.lft.freshordering.service.IOrdersService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tyhxzy
 * @since 2024-12-02
 */
@Service
@Data
public class OrdersServiceImpl implements IOrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Map> selectordersByid(Integer uid) {
        return ordersMapper.selectordersByid(uid);
    }


    @Override
    public OrderVO selectOrders(String oid) {
        // 获取订单信息
        Orders orders = ordersMapper.selectOrders(oid);
        List<Map> maps = ordersMapper.selectOrderDetail(oid);
        Audit audit = ordersMapper.selectAudit(orders.getUid());
        OrderVO order = new OrderVO();
        order.setAudit(audit);
        order.setMaps(maps);
        order.setId(orders.getId());
        order.setPrice(orders.getPrice());
        order.setCreatetime(orders.getCreatetime());
        order.setStatus(orders.getStatus());
        order.setDelivery(orders.getDelivery());
        return order;
    }


    @Override
    public List<OrderVO> selectAllOrders() {
        List<OrderVO> orders1 = new ArrayList<>();
        List<Orders> list = ordersMapper.selectAllOrders();
        System.out.println(list);
        for (Orders orders : list) {
            List<Map> maps = ordersMapper.selectOrderDetail(orders.getId());
            System.out.println(maps);
            Audit audit = ordersMapper.selectAudit(orders.getUid());
            System.out.println(audit);
            OrderVO order = new OrderVO();
            order.setAudit(audit);
            order.setMaps(maps);
            order.setId(orders.getId());
            order.setPrice(orders.getPrice());
            order.setCreatetime(orders.getCreatetime());
            order.setStatus(orders.getStatus());
            orders1.add(order);
            System.out.println(order);
        }
        return orders1;

    }

    @Override
    public List<OrderVO> selectAllOrders1() {
        List<OrderVO> orders1 = new ArrayList<>();
        List<Orders> list = ordersMapper.selectAllOrders1();
        for (Orders orders : list) {
            List<Map> maps = ordersMapper.selectOrderDetail(orders.getId());
            System.out.println(maps);
            Audit audit = ordersMapper.selectAudit(orders.getUid());
            System.out.println(audit);
            OrderVO order = new OrderVO();
            order.setAudit(audit);
            order.setMaps(maps);
            order.setId(orders.getId());
            order.setPrice(orders.getPrice());
            order.setCreatetime(orders.getCreatetime());
            order.setStatus(orders.getStatus());
            orders1.add(order);
            System.out.println(order);
        }
        return orders1;

    }

    @Override
    public List<OrderVO> selectAllOrders2() {
        List<OrderVO> orders1 = new ArrayList<>();
        List<Orders> list = ordersMapper.selectAllOrders2();
        System.out.println(list);
        for (Orders orders : list) {
            List<Map> maps = ordersMapper.selectOrderDetail(orders.getId());
            System.out.println(maps);
            Audit audit = ordersMapper.selectAudit(orders.getUid());
            System.out.println(audit);
            OrderVO order = new OrderVO();
            order.setAudit(audit);
            order.setMaps(maps);
            order.setId(orders.getId());
            order.setPrice(orders.getPrice());
            order.setCreatetime(orders.getCreatetime());
            order.setStatus(orders.getStatus());
            orders1.add(order);
            System.out.println(order);
        }
        return orders1;

    }

    @Override
    public int updateStatus(Integer id) {
        return ordersMapper.updateStatus(id);

    }

    @Override
    public int updateDelivery(byte[] delivery, Integer id) {
        return ordersMapper.updateDelivery(delivery, id);

    }

    @Override
    public int updateStatus1(Integer id) {
        int i = ordersMapper.updateStatus1(id);
        return i;

    }

    @Override
    public List<OrderVO> selectordersByiddym(Integer uid) {

        List<OrderVO> order = new ArrayList<>();
        List<Orders> orders = ordersMapper.selectAllOrdersdym(uid); // 根据查询用户ID查询订单信息
        for (Orders orders1 : orders) {
            List<Map> maps = ordersMapper.selectOrderDetail(orders1.getId());
            Audit audit = ordersMapper.selectAudit(uid);
            order.add(new OrderVO(orders1.getStatus(), orders1.getId(), orders1.getCreatetime(), orders1.getPrice(), orders1.getDelivery(), maps, audit));
        }

        return order;
    }

    @Override
    public int updateOrderStatus(String id, String confirm) {
        return ordersMapper.updateOrderStatusdym(id, confirm);
    }

    @Override
    public List<OrderVO> selectOrders1(Integer uid) {
        List<OrderVO> orders1 = new ArrayList<>();
        List<Orders> list = ordersMapper.selectOrders2(uid);
        System.out.println(list);
        for (Orders orders : list) {
            List<Map> maps = ordersMapper.selectOrderDetail(orders.getId());
            System.out.println(maps);
            Audit audit = ordersMapper.selectAudit(orders.getUid());
            System.out.println(audit);
            OrderVO order = new OrderVO();
            order.setAudit(audit);
            order.setMaps(maps);
            order.setId(orders.getId());
            order.setPrice(orders.getPrice());
            order.setCreatetime(orders.getCreatetime());
            orders1.add(order);
            System.out.println(order);
        }
        return orders1;
    }

    @Override
    public List<OrderVO> selectOrders2(Integer uid) {
        List<OrderVO> orders1 = new ArrayList<>();
        List<Orders> list = ordersMapper.selectOrders1(uid);
        System.out.println(list);
        for (Orders orders : list) {
            List<Map> maps = ordersMapper.selectOrderDetail(orders.getId());
            System.out.println(maps);
            Audit audit = ordersMapper.selectAudit(orders.getUid());
            System.out.println(audit);
            OrderVO order = new OrderVO();
            order.setAudit(audit);
            order.setMaps(maps);
            order.setId(orders.getId());
            order.setPrice(orders.getPrice());
            order.setCreatetime(orders.getCreatetime());
            orders1.add(order);
            System.out.println(order);
        }
        return orders1;
    }


    @Override
    @Scheduled(initialDelay = 10000, fixedRate = 10000) // 启动后延迟10秒，然后每10秒执行一次
    public void autoConfirmOrders() {
        LocalDateTime createTimeThreshold = LocalDateTime.now().minusMinutes(7);
        int updatedCount = ordersMapper.autoConfirmOrders("3", createTimeThreshold);
        System.out.println("自动确认了 " + updatedCount + " 个订单");
    }

//zjb向上
//-----------------------------------------------------------------------------------------------------------------------
//ght向下

    //查询门店账单信息
    @Override
    public BillAllVO selectall() {
        BillAllVO billAllVO = new BillAllVO();//前端返回

        List<AuditVO> auditVOs = new ArrayList();//创建门店信息列表

        Double v = ordersMapper.AllSum();//查询到供应商的总营收
        billAllVO.setSum(v);


        //查询所有的订单基本信息信息
        List<Audit> audits = ordersMapper.selectAll();
        //计算从每个门店中所盈利的价格
        for (Audit audit : audits) {//循环订单
            AuditVO auditVO = new AuditVO();
            //获取对应订单的盈利价格
            Double profit = ordersMapper.profit(audit.getUserId());


            auditVO.setShopname(audit.getShopName());//存入门店的名字
            auditVO.setAddress(audit.getAddress());//存入门店地址
            auditVO.setProfit(profit);//存入盈利数据

            //查询是否结清
            List<priceVO> priceVOS = ordersMapper.selectPay(audit.getUserId());

            ArrayList<Integer> integers = new ArrayList<>();
            for (priceVO item : priceVOS) {
                Double Realpay = item.getRealpay();
                Double Price = item.getPrice();
                Double box = Price - Realpay;
                if (box > 0) {
                    //说明未结清
                    integers.add(1);
                }
            }
            if (integers.size() == 0) {
                auditVO.setPay(0);//0 结清
            } else {
                auditVO.setPay(1);//1 未结清
            }

            auditVOs.add(auditVO);//存入VO集合中
        }
        billAllVO.setAuditVOList(auditVOs);
        return billAllVO;
    }

    //通过用户id查询 用户所拥有的全部订单基本信息
    @Override
    public BilVO selectorderbyuserid(String shopname) {
        //通过商店名字查询用户id
        Integer uid = ordersMapper.selectidbyshopname(shopname);


        BilVO bilVO = new BilVO();//账单VO类
        List<Orders2VO> orders2VOS = new ArrayList<>();

        Double selectsum = ordersMapper.selectsum(uid);//查询该店面累积消费金额
        Double selectrealpay = ordersMapper.selectrealpay(uid);//查询该店面已结清金额
        Double qiankuan = 0.0;
        if (selectrealpay != null) {
            qiankuan = selectsum - selectrealpay;//查询该店面欠款金额
        }

        bilVO.setSum(selectsum);
        bilVO.setRealpay(selectrealpay);
        bilVO.setQiankuan(qiankuan);

        List<Orders2VO> selectsomorders = ordersMapper.selectsomorders(uid);//通过uid 查询此人所有的订单
        for (Orders2VO item : selectsomorders) {
            //创建订单VO类对象
            Orders2VO orders2VO = new Orders2VO();

            orders2VO.setId(item.getId());//存入订单编号
            orders2VO.setCreatetime(item.getCreatetime());//存入创建订单时间

            Double price = item.getPrice();//欠款总计
            Double realpay = item.getRealpay();//实际支付
            Double sy = price - realpay;
            orders2VO.setRealpay(sy);//存入实际支付金额

            String id1 = item.getId();//获取订单编号
            List<OrderDetail> selectshopidbyid = ordersMapper.selectshopidbyid(id1);
            StringBuilder shopNames = new StringBuilder();
            for (OrderDetail item1 : selectshopidbyid) {

                Integer sid = item1.getSid();//商品id
                String selectgoodsbyid = ordersMapper.selectgoodsbyid(sid);
                if (shopNames.length() > 0) {
                    shopNames.append(", ");
                }
                shopNames.append(selectgoodsbyid);
                orders2VO.setShopName(shopNames.toString()); // 设置拼接好的商品名字
            }


            orders2VOS.add(orders2VO);
        }

        bilVO.setOrders(orders2VOS);

        return bilVO;
    }

    @Override
    public int updatePaystate(Map map) {
        String oid = (String) map.get("debtId");//订单编号
        String pid = (String) map.get("paystate");//支付状态

        Integer i = ordersMapper.updateOrderPaystate(oid,pid);

        return i;
    }

    @Override
    public int updateRealpayInt(Orders orders) {
        return 0;
    }

    @Override
    public List<AuditVO> selectorderstaus(Integer id) {
        if (id > 0) {//1未结清 0已结清
            //已结清门店信息
            List<AuditVO> auditVOSList = new ArrayList<>();
            List<Integer> selectweijieqing = ordersMapper.selectweijieqing();
            for (Integer item1 : selectweijieqing) {
                AuditVO aa = ordersMapper.selectbyuid(item1);//通过用户编号查询用户信息
                auditVOSList.add(aa);
            }
            return auditVOSList;
        } else {
            System.out.println(00);
            List<Orders> selectyijieq = ordersMapper.selectyijieq();
            return null;
        }
    }


    @Override
    public Map selectOrderDetail2(String oid) {
        Map map = ordersMapper.selectOrderDetail2(oid);
        List<Map> maps = ordersMapper.selectOrderDetail1(oid);
        map.put("goods", maps);

        return map;
    }

    @Override
    public List<Map> selectOrderDetail1(Map map) {


        return ordersMapper.seleclorders11(map);
    }


    //ght向上
//-----------------------------------------------------------------------------------------------------------------------
//dym向下

    /**
     * 上传订单图片
     * <p>
     * 此方法接收一个Orders对象作为参数，该对象中包含了需要更新的订单信息，
     * 特别是订单的图片信息通过此方法调用updateOrderImage方法更新到数据库中
     *
     * @param orders 包含订单信息的Orders对象，其中包括了待更新的图片信息
     * @return 返回更新操作的影响行数，通常为1表示成功更新，否则可能更新失败或没有找到对应的订单
     */
    @Override
    public int uploadOrderImage(Orders orders) {
        return ordersMapper.updateOrderImage(orders);
    }

    @Override
    public List<Orders> selectid(Integer id) {
        return ordersMapper.selectybyid(id);
    }

    /**
     * 根据订单ID选择订单信息
     * 此方法用于从数据库中检索特定ID的订单信息
     *
     * @param id 订单的唯一标识符
     * @return 返回一个Orders对象列表，列表中包含与给定ID匹配的订单信息如果找不到匹配的订单，则返回空列表
     */


}
